#include "gcgraph.h"

BaseGraph::BaseGraph(uint iWidth, uint iHeight) :
	_iHeight(iHeight), _iWidth(iWidth) {
	VERBOSE("construction graph");

	_aNodes.resize(iWidth);
	for (uint x = 0; x<iWidth; ++x) {
		_aNodes[x].resize(iHeight);
		for (uint y = 0; y<iHeight; ++y) {
			_aNodes[x][y] = _oGraph.add_node();
		}	
	}
}
BaseGraph::~BaseGraph() {
	MAXVERBOSE("destructing BaseGraph");
}
void BaseGraph::addNode(uint x, uint y, const Weights& w) {	
	MAXVERBOSE("adding node");
	std::ostringstream s;
	short int o = 0,b = 0, i = 0; // object, background, within
	s<<"weights:";
	if (x>0) {
		i = int(w.getLeft()*std::numeric_limits<short>::max());
		_oGraph.add_edge(_aNodes[x][y],_aNodes[x-1][y], i,i);
		s<<" Left: "<<i;
	}
	if (y>0) {
		i = int(w.getTop()*std::numeric_limits<short>::max());
		_oGraph.add_edge(_aNodes[x][y],_aNodes[x][y-1],i,i);
		s<<" Top: "<<i;
	}
	o = int(w.getObject()*std::numeric_limits<short>::max());
	b = int(w.getBackground()*std::numeric_limits<short>::max());
	_oGraph.add_tweights(_aNodes[x][y], o, b);
	s<<" Object: "<<o<<" Background: "<<b;
	MAXVERBOSE(s.str());
}
void BaseGraph::maxFlow() {
	VERBOSE("calculating maxflow");
	_oGraph.maxflow();
}
bool BaseGraph::isForeground(uint x, uint y) const {
	return _oGraph.what_segment(_aNodes[x][y]) == Graph::SOURCE;
}
bool BaseGraph::isObject(uint x, uint y)			const {return isForeground(x,y);}
bool BaseGraph::isBackground(uint x, uint y)	const {return !isBackground(x,y);}
